#coding:utf-8
import os
import openpyxl
from common.koufuApi import KoufuApi
from common.koufuShop20Api import KoufuShop20Api
from common.tool import Tools

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
from common.picAddress import PicAddress
from common.Config import Config
import json
import requests

'''
创建商品
goods_save接口多数据回归测试
'''

def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.shop20_domain
    kou_fu_shop_api = KoufuShop20Api()
    script_name = os.path.basename(__file__)
    tool = Tools
    tf = True
    test_result = ""

    ##登陆
    head = {"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}
    p = kou_fu_shop_api.shop_login()
    r = requests.post(test_url + kou_fu_shop_api.shop_login(get_path=1) ,data=p, headers=head)
    reCookie = r.headers.get("set-cookie")
    jsessionid = reCookie[11:47]
    print(jsessionid)
    # kou_fu_conf.koufu_shop_headers["Cookie"] = "JSESSIONID="+jsessionid
    kou_fu_conf.koufu_shop_headers = {"Cookie": "JSESSIONID="+jsessionid,
                                      "Content-Type":"application/json"}

    ##获取测试数据
    # wb = openpyxl.load_workbook("../data/goodsSave1.xlsx")
    excel_uri = Config.caseUri
    wb = openpyxl.load_workbook(excel_uri + "data/goodsSave1.xlsx")
    sheet = wb["goods_save"]
    max_line = sheet.max_row + 1
    test_data_arr = []
    expect_data_arr = []
    for i in range(2, max_line):
        test_data = sheet.cell(i,6).value
        test_data = str(test_data).strip()
        if test_data == "None":
            continue
        # print(test_data)
        test_data_arr.append(test_data)
        expect_data_arr.append(str(sheet.cell(i,7).value).strip())
    print(test_data_arr)

    err_info = ""
    for i in range(len(test_data_arr)):
        iz = str(i).zfill(4)
        # i = "yxmyxmyxm"
        goods_sn = "AT24" + iz
        name = "横行霸道24" + iz
        ##创建产品 PicAddress.pic[7]
        print("test_data:" + test_data_arr[i])
        test_data_obj = eval(test_data_arr[i])
        test_data_obj["primaryPicUrl"] = PicAddress.pic[i]
        test_data_obj["goodsSn"] = goods_sn
        test_data_obj["name"] = name
        p = kou_fu_shop_api.get_goods_save(update_body=test_data_obj)
        r = requests.post(test_url+kou_fu_shop_api.get_goods_save(get_path=1), json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
        print(r.content.decode("utf-8"))
        if  r.content.decode("utf-8") == expect_data_arr[i]:
            print("pass")
        else:
            err_info = err_info + "Error Info: "+test_data_arr[i] + " "
            tf = False
            print("fail")


    #定义预期描述
    print(str(tf))
    if  err_info == "":
        test_result = "所有数据通过测试"
    else:
        test_result = str(err_info)
    print(test_result)
    expect_result = "验证预设的测试数据是否符合预期返回值"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

# case()